package cn.tedu._04weibo.controller;

import cn.tedu._04weibo.base.response.jsonresulte;
import cn.tedu._04weibo.base.response.statuscode;
import cn.tedu._04weibo.mapper.wmapper;
import cn.tedu._04weibo.pojo.entity.weibo;
import cn.tedu._04weibo.pojo.vo.uservo;
import cn.tedu._04weibo.pojo.vo.wdevo;
import cn.tedu._04weibo.pojo.vo.weibovo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/weibo")
@Tag(name="微博管理")
public class wcon
{
    @Autowired
    private wmapper mapper;

    @PostMapping("/add")
    @Operation(summary = "添加微博")
    @ApiOperationSupport(order = 10)
    public jsonresulte add(@RequestBody String content, HttpSession  session)
    {
        log.debug("开始添加微博"+content);
        uservo user=(uservo)session.getAttribute("user");
        if(user==null)
        {return new jsonresulte(statuscode.NOT_LOGIN);}
        weibo w=new weibo();
        w.setContent(content);
        w.setUserId(user.getId());
        w.setCreateTime(new Date());
        int n=mapper.insert(w);
        return jsonresulte.success();

    }

    @GetMapping("/list")
    @Operation(summary = "首页列表")
    @ApiOperationSupport(order = 20)
    public jsonresulte select(HttpSession session)
    {
        uservo user=(uservo)session.getAttribute("user");
        if(user==null)
        {return new jsonresulte(statuscode.NOT_LOGIN);}
        List<weibovo> list=mapper.selectall();
        return jsonresulte.success(list);
    }

    @GetMapping("/add")
    @Operation(summary = "详情")
    @ApiOperationSupport(order = 30)
    public jsonresulte delail(Long id,HttpSession session)
    {
        log.debug("查询id"+ id);
        uservo user=(uservo)session.getAttribute("user");
        if(user==null)
        {return new jsonresulte(statuscode.NOT_LOGIN);}
        wdevo list=mapper.detail(id);
        return jsonresulte.success(list);
    }
}
